{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维数组的索引和切片\n",
    "arr = np.arange(1,9)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问 4 号 下标索引的元素\n",
    "arr[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对 索引1到索引len(arr)-1,每隔一个进行切分\n",
    "arr2 = arr[1:len(arr)-1:2]\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 负索引的使用 \n",
    "# 使用范围是 -len(arr) ~ 0\n",
    "arr[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[-len(arr)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同理切片也可以填入负数\n",
    "arr[-len(arr):-2:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 6, 5, 4, 3, 2, 1])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# step 也是可以填入负数的\n",
    "arr[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组的索引和切片\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第一行第三个元素\n",
    "arr[0,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先获取第二行的元素\n",
    "arr[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  7, 11])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在获取第三列的数据 \n",
    "arr[:,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取全部的元素\n",
    "arr[:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先获取第2行前3个元素\n",
    "arr[1,0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 10])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在获取第2列，后2个元素\n",
    "arr[-2:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3],\n",
       "       [ 6,  7],\n",
       "       [10, 11]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得第2列和第3列的元素\n",
    "arr[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  7],\n",
       "       [10, 11]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在获取 \n",
    "# [\n",
    "# [6,7]\n",
    "# [10,11]\n",
    "#]\n",
    "arr[1:3,1:3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  3],\n",
       "       [ 5,  7],\n",
       "       [ 9, 11]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得第1列和第3列的全部元素\n",
    "arr[:,0:3:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reshape 改变数组的长度\n",
    "arr = np.arange(1,13).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6],\n",
       "       [ 7,  8,  9, 10, 11, 12]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以使用 shape 来指定维度\n",
    "arr.shape = (2,6)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 12 into shape (2,7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[45], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 如果arr的数量和reshape指定的数量不匹配\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43marr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m7\u001b[39m)\n\u001b[0;32m      3\u001b[0m arr\n",
      "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 12 into shape (2,7)"
     ]
    }
   ],
   "source": [
    "# 如果arr的数量和reshape指定的数量不匹配\n",
    "arr.shape = (2,7)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11,  0,  0,  0,  0]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 resize \n",
    "x = np.array(range(12))\n",
    "x.resize((2,8))\n",
    "x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11,  0,  0,  0]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.resize(x,(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.resize(x,(2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6],\n",
       "       [ 7,  8,  9, 10, 11, 12]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把多维改成1维\n",
    "arr = np.arange(1,13).reshape(2,6)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 ravel 方法\n",
    "arr2 = arr.ravel()\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2, 100,   4,   5,   6,   7,   8,   9,  10,  11,  12])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并且修改arr2的值，观察arr的数据是否会发生更改\n",
    "arr2[2]=100\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6],\n",
       "       [ 7,  8,  9, 10, 11, 12]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# arr 对应的的值也发生了修改\n",
    "arr = np.arange(1,13).reshape(2,6)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 现在我们把 arr 的 元素在变回去\n",
    "# 然后观察 flatten函数的使用.\n",
    "arr2 = arr.flatten()\n",
    "arr2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    1,     2,     3, 10000,     5,     6,     7,     8,     9,\n",
       "          10,    11,    12])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改 arr2 的值，再看看arr是否有变化\n",
    "arr2[3] = 10000\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6],\n",
       "       [ 7,  8,  9, 10, 11, 12]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 发现没有变化\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 concatenate 函数进行拼接\n",
    "# 一维数组的拼接\n",
    "arr1 = np.arange(1,5)\n",
    "arr2 = np.arange(11,15)\n",
    "arr3 = np.concatenate((arr1,arr2))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3,   4],\n",
       "       [  5,   6,   7,   8],\n",
       "       [  9,  10,  11,  12],\n",
       "       [101, 102, 103, 104],\n",
       "       [105, 106, 107, 108]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组进行水平拼接  axis = 0,默认是按照 列 堆叠,其中数组的列数必须一样\n",
    "arr1 = np.arange(1,13).reshape(3,4)\n",
    "arr2 = np.arange(101,109).reshape(2,4)\n",
    "ret = np.concatenate((arr1,arr2))\n",
    "ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3,   4, 101, 102],\n",
       "       [  5,   6,   7,   8, 103, 104],\n",
       "       [  9,  10,  11,  12, 105, 106]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis = 1,默认是按照 行 堆叠,其中数组的行数必须一样\n",
    "arr1 = np.arange(1,13).reshape(3,4)\n",
    "arr2 = np.arange(101,107).reshape(3,2)\n",
    "ret = np.concatenate((arr1,arr2),axis=1)\n",
    "ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3,   4, 101, 102],\n",
       "       [  5,   6,   7,   8, 103, 104],\n",
       "       [  9,  10,  11,  12, 105, 106]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 hstack 函数进行堆叠 水平的\n",
    "arr1 = np.arange(1,13).reshape(3,4)\n",
    "arr2 = np.arange(101,107).reshape(3,2)\n",
    "ret = np.hstack([arr1,arr2])\n",
    "ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3,   4],\n",
       "       [  5,   6,   7,   8],\n",
       "       [  9,  10,  11,  12],\n",
       "       [101, 102, 103, 104],\n",
       "       [105, 106, 107, 108]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 vstack 函数进行堆叠 垂直的\n",
    "arr1 = np.arange(1,13).reshape(3,4)\n",
    "arr2 = np.arange(101,109).reshape(2,4)\n",
    "ret = np.vstack([arr1,arr2])\n",
    "ret"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的分隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 split 进行分隔\n",
    "# 首先先进行一维数组的分隔\n",
    "arr = np.arange(1,11)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([1, 2, 3, 4, 5]), array([ 6,  7,  8,  9, 10])]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二个参数是整数的情况,返回的列表\n",
    "np.split(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([1, 2]), array([3, 4, 5]), array([ 6,  7,  8,  9, 10])]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 传入数组，左闭右开\n",
    "np.split(ary=arr,indices_or_sections=[2,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对二维数组进行分隔\n",
    "arr = np.arange(1,21).reshape(4,5)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10]]),\n",
       " array([[11, 12, 13, 14, 15],\n",
       "        [16, 17, 18, 19, 20]])]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二个参数为数字，然后按照 行 进行分隔\n",
    "np.split(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "array split does not result in an equal division",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[80], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 按照列进行分隔\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\software\\Anaconda\\envs\\Data_Analysis\\Lib\\site-packages\\numpy\\lib\\shape_base.py:864\u001b[0m, in \u001b[0;36msplit\u001b[1;34m(ary, indices_or_sections, axis)\u001b[0m\n\u001b[0;32m    862\u001b[0m     N \u001b[38;5;241m=\u001b[39m ary\u001b[38;5;241m.\u001b[39mshape[axis]\n\u001b[0;32m    863\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m N \u001b[38;5;241m%\u001b[39m sections:\n\u001b[1;32m--> 864\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    865\u001b[0m             \u001b[38;5;124m'\u001b[39m\u001b[38;5;124marray split does not result in an equal division\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m    866\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m array_split(ary, indices_or_sections, axis)\n",
      "\u001b[1;31mValueError\u001b[0m: array split does not result in an equal division"
     ]
    }
   ],
   "source": [
    "# 按照列进行分隔,如果 传递的数字，必须是可以整除的，不然会报错：\n",
    "#  array split does not result in an equal division\n",
    "np.split(arr,2,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5]]),\n",
       " array([[ 6,  7,  8,  9, 10],\n",
       "        [11, 12, 13, 14, 15]]),\n",
       " array([[16, 17, 18, 19, 20]])]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二个参数传入数组 # 默认按照行进行分隔\n",
    "np.split(arr,[1,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5]]),\n",
       " array([[ 6,  7,  8,  9, 10],\n",
       "        [11, 12, 13, 14, 15],\n",
       "        [16, 17, 18, 19, 20]]),\n",
       " array([], shape=(0, 5), dtype=int32)]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果传递的数组右值大于了数组的行数呢\n",
    "# 不会报错，而是最后一个分隔的数组为空\n",
    "np.split(arr,[1,len(arr)+1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1],\n",
       "        [ 6],\n",
       "        [11],\n",
       "        [16]]),\n",
       " array([[ 2,  3,  4],\n",
       "        [ 7,  8,  9],\n",
       "        [12, 13, 14],\n",
       "        [17, 18, 19]]),\n",
       " array([[ 5],\n",
       "        [10],\n",
       "        [15],\n",
       "        [20]])]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照列进行分隔\n",
    "np.split(arr,[1,4],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1],\n",
       "        [ 6],\n",
       "        [11],\n",
       "        [16]]),\n",
       " array([[ 2,  3,  4,  5],\n",
       "        [ 7,  8,  9, 10],\n",
       "        [12, 13, 14, 15],\n",
       "        [17, 18, 19, 20]]),\n",
       " array([], shape=(4, 0), dtype=int32)]"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同理  如果传递的数组右值大于了数组的列数呢\n",
    "# 不会报错，而是最后一个分隔的数组为空\n",
    "np.split(arr,[1,len(arr)+1],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1],\n",
       "        [ 6],\n",
       "        [11],\n",
       "        [16]]),\n",
       " array([[ 2],\n",
       "        [ 7],\n",
       "        [12],\n",
       "        [17]]),\n",
       " array([[ 3],\n",
       "        [ 8],\n",
       "        [13],\n",
       "        [18]]),\n",
       " array([[ 4],\n",
       "        [ 9],\n",
       "        [14],\n",
       "        [19]]),\n",
       " array([[ 5],\n",
       "        [10],\n",
       "        [15],\n",
       "        [20]])]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# hsplit() 按照列进行分隔\n",
    "np.hsplit(arr,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1],\n",
       "        [ 6],\n",
       "        [11],\n",
       "        [16]]),\n",
       " array([[ 2,  3,  4],\n",
       "        [ 7,  8,  9],\n",
       "        [12, 13, 14],\n",
       "        [17, 18, 19]]),\n",
       " array([[ 5],\n",
       "        [10],\n",
       "        [15],\n",
       "        [20]])]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(arr,[1,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10]]),\n",
       " array([[11, 12, 13, 14, 15],\n",
       "        [16, 17, 18, 19, 20]])]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 vsplit 按行进行分隔\n",
    "np.vsplit(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5]]),\n",
       " array([[ 6,  7,  8,  9, 10],\n",
       "        [11, 12, 13, 14, 15]]),\n",
       " array([[16, 17, 18, 19, 20]])]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(arr,[1,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 6, 6, 4, 2, 6, 5])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(1,10,7)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 4, 5, 6, 6, 6])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 sort 进行排序,默认是升序排列 \n",
    "np.sort(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 6, 6, 4, 2, 6, 5])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并不会改变原来数组的值\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m       \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mCall signature:\u001b[0m  \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mType:\u001b[0m            _ArrayFunctionDispatcher\n",
      "\u001b[1;31mString form:\u001b[0m     <function sort at 0x000001F1ABCC9F80>\n",
      "\u001b[1;31mFile:\u001b[0m            d:\\software\\anaconda\\envs\\data_analysis\\lib\\site-packages\\numpy\\core\\fromnumeric.py\n",
      "\u001b[1;31mSource:\u001b[0m         \n",
      "\u001b[1;33m@\u001b[0m\u001b[0marray_function_dispatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_sort_dispatcher\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m\u001b[1;32mdef\u001b[0m \u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;34m\"\"\"\n",
      "    Return a sorted copy of an array.\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    a : array_like\n",
      "        Array to be sorted.\n",
      "    axis : int or None, optional\n",
      "        Axis along which to sort. If None, the array is flattened before\n",
      "        sorting. The default is -1, which sorts along the last axis.\n",
      "    kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional\n",
      "        Sorting algorithm. The default is 'quicksort'. Note that both 'stable'\n",
      "        and 'mergesort' use timsort or radix sort under the covers and, in general,\n",
      "        the actual implementation will vary with data type. The 'mergesort' option\n",
      "        is retained for backwards compatibility.\n",
      "\n",
      "        .. versionchanged:: 1.15.0.\n",
      "           The 'stable' option was added.\n",
      "\n",
      "    order : str or list of str, optional\n",
      "        When `a` is an array with fields defined, this argument specifies\n",
      "        which fields to compare first, second, etc.  A single field can\n",
      "        be specified as a string, and not all fields need be specified,\n",
      "        but unspecified fields will still be used, in the order in which\n",
      "        they come up in the dtype, to break ties.\n",
      "\n",
      "    Returns\n",
      "    -------\n",
      "    sorted_array : ndarray\n",
      "        Array of the same type and shape as `a`.\n",
      "\n",
      "    See Also\n",
      "    --------\n",
      "    ndarray.sort : Method to sort an array in-place.\n",
      "    argsort : Indirect sort.\n",
      "    lexsort : Indirect stable sort on multiple keys.\n",
      "    searchsorted : Find elements in a sorted array.\n",
      "    partition : Partial sort.\n",
      "\n",
      "    Notes\n",
      "    -----\n",
      "    The various sorting algorithms are characterized by their average speed,\n",
      "    worst case performance, work space size, and whether they are stable. A\n",
      "    stable sort keeps items with the same key in the same relative\n",
      "    order. The four algorithms implemented in NumPy have the following\n",
      "    properties:\n",
      "\n",
      "    =========== ======= ============= ============ ========\n",
      "       kind      speed   worst case    work space   stable\n",
      "    =========== ======= ============= ============ ========\n",
      "    'quicksort'    1     O(n^2)            0          no\n",
      "    'heapsort'     3     O(n*log(n))       0          no\n",
      "    'mergesort'    2     O(n*log(n))      ~n/2        yes\n",
      "    'timsort'      2     O(n*log(n))      ~n/2        yes\n",
      "    =========== ======= ============= ============ ========\n",
      "\n",
      "    .. note:: The datatype determines which of 'mergesort' or 'timsort'\n",
      "       is actually used, even if 'mergesort' is specified. User selection\n",
      "       at a finer scale is not currently available.\n",
      "\n",
      "    All the sort algorithms make temporary copies of the data when\n",
      "    sorting along any but the last axis.  Consequently, sorting along\n",
      "    the last axis is faster and uses less space than sorting along\n",
      "    any other axis.\n",
      "\n",
      "    The sort order for complex numbers is lexicographic. If both the real\n",
      "    and imaginary parts are non-nan then the order is determined by the\n",
      "    real parts except when they are equal, in which case the order is\n",
      "    determined by the imaginary parts.\n",
      "\n",
      "    Previous to numpy 1.4.0 sorting real and complex arrays containing nan\n",
      "    values led to undefined behaviour. In numpy versions >= 1.4.0 nan\n",
      "    values are sorted to the end. The extended sort order is:\n",
      "\n",
      "      * Real: [R, nan]\n",
      "      * Complex: [R + Rj, R + nanj, nan + Rj, nan + nanj]\n",
      "\n",
      "    where R is a non-nan real value. Complex values with the same nan\n",
      "    placements are sorted according to the non-nan part if it exists.\n",
      "    Non-nan values are sorted as before.\n",
      "\n",
      "    .. versionadded:: 1.12.0\n",
      "\n",
      "    quicksort has been changed to `introsort <https://en.wikipedia.org/wiki/Introsort>`_.\n",
      "    When sorting does not make enough progress it switches to\n",
      "    `heapsort <https://en.wikipedia.org/wiki/Heapsort>`_.\n",
      "    This implementation makes quicksort O(n*log(n)) in the worst case.\n",
      "\n",
      "    'stable' automatically chooses the best stable sorting algorithm\n",
      "    for the data type being sorted.\n",
      "    It, along with 'mergesort' is currently mapped to\n",
      "    `timsort <https://en.wikipedia.org/wiki/Timsort>`_\n",
      "    or `radix sort <https://en.wikipedia.org/wiki/Radix_sort>`_\n",
      "    depending on the data type.\n",
      "    API forward compatibility currently limits the\n",
      "    ability to select the implementation and it is hardwired for the different\n",
      "    data types.\n",
      "\n",
      "    .. versionadded:: 1.17.0\n",
      "\n",
      "    Timsort is added for better performance on already or nearly\n",
      "    sorted data. On random data timsort is almost identical to\n",
      "    mergesort. It is now used for stable sort while quicksort is still the\n",
      "    default sort if none is chosen. For timsort details, refer to\n",
      "    `CPython listsort.txt <https://github.com/python/cpython/blob/3.7/Objects/listsort.txt>`_.\n",
      "    'mergesort' and 'stable' are mapped to radix sort for integer data types. Radix sort is an\n",
      "    O(n) sort instead of O(n log n).\n",
      "\n",
      "    .. versionchanged:: 1.18.0\n",
      "\n",
      "    NaT now sorts to the end of arrays for consistency with NaN.\n",
      "\n",
      "    Examples\n",
      "    --------\n",
      "    >>> a = np.array([[1,4],[3,1]])\n",
      "    >>> np.sort(a)                # sort along the last axis\n",
      "    array([[1, 4],\n",
      "           [1, 3]])\n",
      "    >>> np.sort(a, axis=None)     # sort the flattened array\n",
      "    array([1, 1, 3, 4])\n",
      "    >>> np.sort(a, axis=0)        # sort along the first axis\n",
      "    array([[1, 1],\n",
      "           [3, 4]])\n",
      "\n",
      "    Use the `order` keyword to specify a field to use when sorting a\n",
      "    structured array:\n",
      "\n",
      "    >>> dtype = [('name', 'S10'), ('height', float), ('age', int)]\n",
      "    >>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),\n",
      "    ...           ('Galahad', 1.7, 38)]\n",
      "    >>> a = np.array(values, dtype=dtype)       # create a structured array\n",
      "    >>> np.sort(a, order='height')                        # doctest: +SKIP\n",
      "    array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),\n",
      "           ('Lancelot', 1.8999999999999999, 38)],\n",
      "          dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])\n",
      "\n",
      "    Sort by age, then height if ages are equal:\n",
      "\n",
      "    >>> np.sort(a, order=['age', 'height'])               # doctest: +SKIP\n",
      "    array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),\n",
      "           ('Arthur', 1.8, 41)],\n",
      "          dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])\n",
      "\n",
      "    \"\"\"\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[1;31m# flatten returns (1, N) for np.matrix, so always use the last axis\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflatten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"K\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mClass docstring:\u001b[0m\n",
      "Class to wrap functions with checks for __array_function__ overrides.\n",
      "\n",
      "All arguments are required, and can only be passed by position.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "dispatcher : function or None\n",
      "    The dispatcher function that returns a single sequence-like object\n",
      "    of all arguments relevant.  It must have the same signature (except\n",
      "    the default values) as the actual implementation.\n",
      "    If ``None``, this is a ``like=`` dispatcher and the\n",
      "    ``_ArrayFunctionDispatcher`` must be called with ``like`` as the\n",
      "    first (additional and positional) argument.\n",
      "implementation : function\n",
      "    Function that implements the operation on NumPy arrays without\n",
      "    overrides.  Arguments passed calling the ``_ArrayFunctionDispatcher``\n",
      "    will be forwarded to this (and the ``dispatcher``) as if using\n",
      "    ``*args, **kwargs``.\n",
      "\n",
      "Attributes\n",
      "----------\n",
      "_implementation : function\n",
      "    The original implementation passed in."
     ]
    }
   ],
   "source": [
    "# 降序排列\n",
    "# 不能使用 np自带的 sort 函数，因为 他的参数不支持降序\n",
    "np.sort??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 6, 6, 4, 2, 6, 5])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 4, 5, 6, 6, 6])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort() # 使用这个方法，会对自己进行排序，会改变原来的顺序\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 6, 6, 5, 4, 2, 2])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一般不这样写\n",
    "arr = abs(np.sort(-arr))  # 先取相反数排序，再加上绝对值得到原数组的降序\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2, 8, 1, 1, 2, 9])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 argsort()\n",
    "arr = np.random.randint(1,10,7)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 1, 5, 0, 2, 6], dtype=int64)"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得是下标\n",
    "index = np.argsort(arr)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 2, 2, 3, 8, 9])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 再获得排完序的数组\n",
    "arr[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mDocstring:\u001b[0m\n",
      "a.partition(kth, axis=-1, kind='introselect', order=None)\n",
      "\n",
      "Rearranges the elements in the array in such a way that the value of the\n",
      "element in kth position is in the position it would be in a sorted array.\n",
      "All elements smaller than the kth element are moved before this element and\n",
      "all equal or greater are moved behind it. The ordering of the elements in\n",
      "the two partitions is undefined.\n",
      "\n",
      ".. versionadded:: 1.8.0\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "kth : int or sequence of ints\n",
      "    Element index to partition by. The kth element value will be in its\n",
      "    final sorted position and all smaller elements will be moved before it\n",
      "    and all equal or greater elements behind it.\n",
      "    The order of all elements in the partitions is undefined.\n",
      "    If provided with a sequence of kth it will partition all elements\n",
      "    indexed by kth of them into their sorted position at once.\n",
      "\n",
      "    .. deprecated:: 1.22.0\n",
      "        Passing booleans as index is deprecated.\n",
      "axis : int, optional\n",
      "    Axis along which to sort. Default is -1, which means sort along the\n",
      "    last axis.\n",
      "kind : {'introselect'}, optional\n",
      "    Selection algorithm. Default is 'introselect'.\n",
      "order : str or list of str, optional\n",
      "    When `a` is an array with fields defined, this argument specifies\n",
      "    which fields to compare first, second, etc. A single field can\n",
      "    be specified as a string, and not all fields need to be specified,\n",
      "    but unspecified fields will still be used, in the order in which\n",
      "    they come up in the dtype, to break ties.\n",
      "\n",
      "See Also\n",
      "--------\n",
      "numpy.partition : Return a partitioned copy of an array.\n",
      "argpartition : Indirect partition.\n",
      "sort : Full sort.\n",
      "\n",
      "Notes\n",
      "-----\n",
      "See ``np.partition`` for notes on the different algorithms.\n",
      "\n",
      "Examples\n",
      "--------\n",
      ">>> a = np.array([3, 4, 2, 1])\n",
      ">>> a.partition(3)\n",
      ">>> a\n",
      "array([2, 1, 3, 4])\n",
      "\n",
      ">>> a.partition((1, 3))\n",
      ">>> a\n",
      "array([1, 2, 3, 4])\n",
      "\u001b[1;31mType:\u001b[0m      builtin_function_or_method"
     ]
    }
   ],
   "source": [
    "# 使用 partition()\n",
    "arr.partition??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([34, 46, 29, 35, 31, 16, 36, 24, 23, 39, 32,  6, 11, 17, 24])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 partition()\n",
    "arr = np.random.randint(1,50,15)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 16,  6, 17, 23, 24, 24, 46, 36, 39, 32, 31, 35, 29, 34])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 例如我现在要知道第5小的数字，并且不用 sort 或者 argsort函数\n",
    "# 因为数组的下标是从 0 开始的，所以 第5小的数字对应的索引下标是4\n",
    "arr.partition(4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第5小的数字\n",
    "arr[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[36, 69, 15, 46, 49, 41],\n",
       "       [75, 74, 13, 64, 92, 62],\n",
       "       [20, 17, 75,  2, 95, 18],\n",
       "       [73,  6, 69, 62, 67, 95]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维数组中使用 sort函数\n",
    "arr = np.random.randint(1,100,size=(4,6))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m       \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mCall signature:\u001b[0m  \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mType:\u001b[0m            _ArrayFunctionDispatcher\n",
      "\u001b[1;31mString form:\u001b[0m     <function sort at 0x000001F1ABCC9F80>\n",
      "\u001b[1;31mFile:\u001b[0m            d:\\software\\anaconda\\envs\\data_analysis\\lib\\site-packages\\numpy\\core\\fromnumeric.py\n",
      "\u001b[1;31mSource:\u001b[0m         \n",
      "\u001b[1;33m@\u001b[0m\u001b[0marray_function_dispatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_sort_dispatcher\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m\u001b[1;32mdef\u001b[0m \u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;34m\"\"\"\n",
      "    Return a sorted copy of an array.\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    a : array_like\n",
      "        Array to be sorted.\n",
      "    axis : int or None, optional\n",
      "        Axis along which to sort. If None, the array is flattened before\n",
      "        sorting. The default is -1, which sorts along the last axis.\n",
      "    kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional\n",
      "        Sorting algorithm. The default is 'quicksort'. Note that both 'stable'\n",
      "        and 'mergesort' use timsort or radix sort under the covers and, in general,\n",
      "        the actual implementation will vary with data type. The 'mergesort' option\n",
      "        is retained for backwards compatibility.\n",
      "\n",
      "        .. versionchanged:: 1.15.0.\n",
      "           The 'stable' option was added.\n",
      "\n",
      "    order : str or list of str, optional\n",
      "        When `a` is an array with fields defined, this argument specifies\n",
      "        which fields to compare first, second, etc.  A single field can\n",
      "        be specified as a string, and not all fields need be specified,\n",
      "        but unspecified fields will still be used, in the order in which\n",
      "        they come up in the dtype, to break ties.\n",
      "\n",
      "    Returns\n",
      "    -------\n",
      "    sorted_array : ndarray\n",
      "        Array of the same type and shape as `a`.\n",
      "\n",
      "    See Also\n",
      "    --------\n",
      "    ndarray.sort : Method to sort an array in-place.\n",
      "    argsort : Indirect sort.\n",
      "    lexsort : Indirect stable sort on multiple keys.\n",
      "    searchsorted : Find elements in a sorted array.\n",
      "    partition : Partial sort.\n",
      "\n",
      "    Notes\n",
      "    -----\n",
      "    The various sorting algorithms are characterized by their average speed,\n",
      "    worst case performance, work space size, and whether they are stable. A\n",
      "    stable sort keeps items with the same key in the same relative\n",
      "    order. The four algorithms implemented in NumPy have the following\n",
      "    properties:\n",
      "\n",
      "    =========== ======= ============= ============ ========\n",
      "       kind      speed   worst case    work space   stable\n",
      "    =========== ======= ============= ============ ========\n",
      "    'quicksort'    1     O(n^2)            0          no\n",
      "    'heapsort'     3     O(n*log(n))       0          no\n",
      "    'mergesort'    2     O(n*log(n))      ~n/2        yes\n",
      "    'timsort'      2     O(n*log(n))      ~n/2        yes\n",
      "    =========== ======= ============= ============ ========\n",
      "\n",
      "    .. note:: The datatype determines which of 'mergesort' or 'timsort'\n",
      "       is actually used, even if 'mergesort' is specified. User selection\n",
      "       at a finer scale is not currently available.\n",
      "\n",
      "    All the sort algorithms make temporary copies of the data when\n",
      "    sorting along any but the last axis.  Consequently, sorting along\n",
      "    the last axis is faster and uses less space than sorting along\n",
      "    any other axis.\n",
      "\n",
      "    The sort order for complex numbers is lexicographic. If both the real\n",
      "    and imaginary parts are non-nan then the order is determined by the\n",
      "    real parts except when they are equal, in which case the order is\n",
      "    determined by the imaginary parts.\n",
      "\n",
      "    Previous to numpy 1.4.0 sorting real and complex arrays containing nan\n",
      "    values led to undefined behaviour. In numpy versions >= 1.4.0 nan\n",
      "    values are sorted to the end. The extended sort order is:\n",
      "\n",
      "      * Real: [R, nan]\n",
      "      * Complex: [R + Rj, R + nanj, nan + Rj, nan + nanj]\n",
      "\n",
      "    where R is a non-nan real value. Complex values with the same nan\n",
      "    placements are sorted according to the non-nan part if it exists.\n",
      "    Non-nan values are sorted as before.\n",
      "\n",
      "    .. versionadded:: 1.12.0\n",
      "\n",
      "    quicksort has been changed to `introsort <https://en.wikipedia.org/wiki/Introsort>`_.\n",
      "    When sorting does not make enough progress it switches to\n",
      "    `heapsort <https://en.wikipedia.org/wiki/Heapsort>`_.\n",
      "    This implementation makes quicksort O(n*log(n)) in the worst case.\n",
      "\n",
      "    'stable' automatically chooses the best stable sorting algorithm\n",
      "    for the data type being sorted.\n",
      "    It, along with 'mergesort' is currently mapped to\n",
      "    `timsort <https://en.wikipedia.org/wiki/Timsort>`_\n",
      "    or `radix sort <https://en.wikipedia.org/wiki/Radix_sort>`_\n",
      "    depending on the data type.\n",
      "    API forward compatibility currently limits the\n",
      "    ability to select the implementation and it is hardwired for the different\n",
      "    data types.\n",
      "\n",
      "    .. versionadded:: 1.17.0\n",
      "\n",
      "    Timsort is added for better performance on already or nearly\n",
      "    sorted data. On random data timsort is almost identical to\n",
      "    mergesort. It is now used for stable sort while quicksort is still the\n",
      "    default sort if none is chosen. For timsort details, refer to\n",
      "    `CPython listsort.txt <https://github.com/python/cpython/blob/3.7/Objects/listsort.txt>`_.\n",
      "    'mergesort' and 'stable' are mapped to radix sort for integer data types. Radix sort is an\n",
      "    O(n) sort instead of O(n log n).\n",
      "\n",
      "    .. versionchanged:: 1.18.0\n",
      "\n",
      "    NaT now sorts to the end of arrays for consistency with NaN.\n",
      "\n",
      "    Examples\n",
      "    --------\n",
      "    >>> a = np.array([[1,4],[3,1]])\n",
      "    >>> np.sort(a)                # sort along the last axis\n",
      "    array([[1, 4],\n",
      "           [1, 3]])\n",
      "    >>> np.sort(a, axis=None)     # sort the flattened array\n",
      "    array([1, 1, 3, 4])\n",
      "    >>> np.sort(a, axis=0)        # sort along the first axis\n",
      "    array([[1, 1],\n",
      "           [3, 4]])\n",
      "\n",
      "    Use the `order` keyword to specify a field to use when sorting a\n",
      "    structured array:\n",
      "\n",
      "    >>> dtype = [('name', 'S10'), ('height', float), ('age', int)]\n",
      "    >>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),\n",
      "    ...           ('Galahad', 1.7, 38)]\n",
      "    >>> a = np.array(values, dtype=dtype)       # create a structured array\n",
      "    >>> np.sort(a, order='height')                        # doctest: +SKIP\n",
      "    array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),\n",
      "           ('Lancelot', 1.8999999999999999, 38)],\n",
      "          dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])\n",
      "\n",
      "    Sort by age, then height if ages are equal:\n",
      "\n",
      "    >>> np.sort(a, order=['age', 'height'])               # doctest: +SKIP\n",
      "    array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),\n",
      "           ('Arthur', 1.8, 41)],\n",
      "          dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])\n",
      "\n",
      "    \"\"\"\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[1;31m# flatten returns (1, N) for np.matrix, so always use the last axis\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflatten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
      "\u001b[0m        \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"K\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\n",
      "\u001b[0m    \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mClass docstring:\u001b[0m\n",
      "Class to wrap functions with checks for __array_function__ overrides.\n",
      "\n",
      "All arguments are required, and can only be passed by position.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "dispatcher : function or None\n",
      "    The dispatcher function that returns a single sequence-like object\n",
      "    of all arguments relevant.  It must have the same signature (except\n",
      "    the default values) as the actual implementation.\n",
      "    If ``None``, this is a ``like=`` dispatcher and the\n",
      "    ``_ArrayFunctionDispatcher`` must be called with ``like`` as the\n",
      "    first (additional and positional) argument.\n",
      "implementation : function\n",
      "    Function that implements the operation on NumPy arrays without\n",
      "    overrides.  Arguments passed calling the ``_ArrayFunctionDispatcher``\n",
      "    will be forwarded to this (and the ``dispatcher``) as if using\n",
      "    ``*args, **kwargs``.\n",
      "\n",
      "Attributes\n",
      "----------\n",
      "_implementation : function\n",
      "    The original implementation passed in."
     ]
    }
   ],
   "source": [
    "np.sort??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 36, 41, 46, 49, 69],\n",
       "       [13, 62, 64, 74, 75, 92],\n",
       "       [ 2, 17, 18, 20, 75, 95],\n",
       "       [ 6, 62, 67, 69, 73, 95]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先按照行进行排序 默认是按照行进行\n",
    "np.sort(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20,  6, 13,  2, 49, 18],\n",
       "       [36, 17, 15, 46, 67, 41],\n",
       "       [73, 69, 69, 62, 92, 62],\n",
       "       [75, 74, 75, 64, 95, 95]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照列进行排序\n",
    "np.sort(arr,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数学函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算三角函数值\n",
    "arr = np.arange(15).reshape(3,5)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ],\n",
       "       [-0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849],\n",
       "       [-0.54402111, -0.99999021, -0.53657292,  0.42016704,  0.99060736]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分别计算 sin,cos,tan\n",
    "np.sin(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.54030231, -0.41614684, -0.9899925 , -0.65364362],\n",
       "       [ 0.28366219,  0.96017029,  0.75390225, -0.14550003, -0.91113026],\n",
       "       [-0.83907153,  0.0044257 ,  0.84385396,  0.90744678,  0.13673722]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00000000e+00,  1.55740772e+00, -2.18503986e+00,\n",
       "        -1.42546543e-01,  1.15782128e+00],\n",
       "       [-3.38051501e+00, -2.91006191e-01,  8.71447983e-01,\n",
       "        -6.79971146e+00, -4.52315659e-01],\n",
       "       [ 6.48360827e-01, -2.25950846e+02, -6.35859929e-01,\n",
       "         4.63021133e-01,  7.24460662e+00]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tan(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.42772947, -1.07062371,  2.55700955],\n",
       "       [ 2.87709978,  2.07859092,  2.2152202 ]])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 四舍五入\n",
    "arr = np.random.normal(loc=3,scale=2,size=(2,3))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.43, -1.07,  2.56],\n",
       "       [ 2.88,  2.08,  2.22]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 round() 可以指定保留的小数位数\n",
    "np.round(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2., -1.,  3.],\n",
       "       [ 3.,  2.,  2.]])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 rint() 四舍五入保留到整数\n",
    "np.rint(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3., -1.,  3.],\n",
       "       [ 3.,  3.,  3.]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ceil()向上取整\n",
    "np.ceil(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2., -2.,  2.],\n",
       "       [ 2.,  2.,  2.]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# floor()向下取整\n",
    "np.floor(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算绝对值或者是复数的模\n",
    "arr1 = np.array([-1,2,4,1.4,-3.2,-np.pi])\n",
    "arr2 = np.array([3+4j,5+6j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 2.        , 4.        , 1.4       , 3.2       ,\n",
       "       3.14159265])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.absolute(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.        , 7.81024968])"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.absolute(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 平方根,立方根\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.41421356, 1.73205081, 2.        ],\n",
       "       [2.23606798, 2.44948974, 2.64575131, 2.82842712],\n",
       "       [3.        , 3.16227766, 3.31662479, 3.46410162]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.25992105, 1.44224957, 1.58740105],\n",
       "       [1.70997595, 1.81712059, 1.91293118, 2.        ],\n",
       "       [2.08008382, 2.15443469, 2.22398009, 2.28942849]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cbrt(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   4,   9,  16],\n",
       "       [ 25,  36,  49,  64],\n",
       "       [ 81, 100, 121, 144]])"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 平方\n",
    "np.square(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 6, 1, 3, 6],\n",
       "       [6, 4, 8, 3, 7],\n",
       "       [4, 7, 4, 1, 8]])"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差分,以e为底数,倒数\n",
    "arr = np.array([[4,6,1,3,6],[6,4,8,3,7],[4,7,4,1,8]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2, -5,  2,  3],\n",
       "       [-2,  4, -5,  4],\n",
       "       [ 3, -3, -3,  7]])"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 算一阶差分 # 默认是按行\n",
    "np.diff(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2, -2,  7,  0,  1],\n",
       "       [-2,  3, -4, -2,  1]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以指定 按照列算差分\n",
    "np.diff(arr,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-7,  7,  1],\n",
       "       [ 6, -9,  9],\n",
       "       [-6,  0, 10]])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以算二阶差分\n",
    "np.diff(np.diff(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  2.71828183,  7.3890561 , 20.08553692, 54.59815003])"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以e为底数\n",
    "np.exp(range(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 倒数\n",
    "# array数组中的数值类型是什么，那么返回的也是相同的类型\n",
    "arr = np.arange(1,10,dtype=int)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回同arr一样的数值类型\n",
    "np.reciprocal(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5., 6., 7., 8., 9.])"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把数值类型修改一下，观察效果\n",
    "arr = np.arange(1,10,dtype=float)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 0.5       , 0.33333333, 0.25      , 0.2       ,\n",
       "       0.16666667, 0.14285714, 0.125     , 0.11111111])"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reciprocal(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 最大公因数,最小公倍数\n",
    "arr1 = np.arange(1,7)\n",
    "arr2 = np.array([3,3,3,2,2,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 3, 2, 1, 2])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最大公因数\n",
    "np.gcd(arr1,arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  6,  3,  4, 10,  6])"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最小公倍数\n",
    "np.lcm(arr1,arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  0, -1,  1,  0,  1], dtype=int32)"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求余数，最大值，最小值\n",
    "# 指定 除数\n",
    "arr = np.array([-3,-2,-1,1,2,3])\n",
    "np.fmod(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 3])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果第二个参数是数组，那么就是对应位置进行求余，返回一个新的数组\n",
    "np.fmod([4,7,9],[3,2,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31mSignature:\u001b[0m       \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mType:\u001b[0m            ufunc\n",
      "\u001b[1;31mString form:\u001b[0m     <ufunc 'fmax'>\n",
      "\u001b[1;31mFile:\u001b[0m            d:\\software\\anaconda\\envs\\data_analysis\\lib\\site-packages\\numpy\\__init__.py\n",
      "\u001b[1;31mDocstring:\u001b[0m      \n",
      "fmax(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])\n",
      "\n",
      "Element-wise maximum of array elements.\n",
      "\n",
      "Compare two arrays and return a new array containing the element-wise\n",
      "maxima. If one of the elements being compared is a NaN, then the\n",
      "non-nan element is returned. If both elements are NaNs then the first\n",
      "is returned.  The latter distinction is important for complex NaNs,\n",
      "which are defined as at least one of the real or imaginary parts being\n",
      "a NaN. The net effect is that NaNs are ignored when possible.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "x1, x2 : array_like\n",
      "    The arrays holding the elements to be compared.\n",
      "    If ``x1.shape != x2.shape``, they must be broadcastable to a common\n",
      "    shape (which becomes the shape of the output).\n",
      "out : ndarray, None, or tuple of ndarray and None, optional\n",
      "    A location into which the result is stored. If provided, it must have\n",
      "    a shape that the inputs broadcast to. If not provided or None,\n",
      "    a freshly-allocated array is returned. A tuple (possible only as a\n",
      "    keyword argument) must have length equal to the number of outputs.\n",
      "where : array_like, optional\n",
      "    This condition is broadcast over the input. At locations where the\n",
      "    condition is True, the `out` array will be set to the ufunc result.\n",
      "    Elsewhere, the `out` array will retain its original value.\n",
      "    Note that if an uninitialized `out` array is created via the default\n",
      "    ``out=None``, locations within it where the condition is False will\n",
      "    remain uninitialized.\n",
      "**kwargs\n",
      "    For other keyword-only arguments, see the\n",
      "    :ref:`ufunc docs <ufuncs.kwargs>`.\n",
      "\n",
      "Returns\n",
      "-------\n",
      "y : ndarray or scalar\n",
      "    The maximum of `x1` and `x2`, element-wise.\n",
      "    This is a scalar if both `x1` and `x2` are scalars.\n",
      "\n",
      "See Also\n",
      "--------\n",
      "fmin :\n",
      "    Element-wise minimum of two arrays, ignores NaNs.\n",
      "maximum :\n",
      "    Element-wise maximum of two arrays, propagates NaNs.\n",
      "amax :\n",
      "    The maximum value of an array along a given axis, propagates NaNs.\n",
      "nanmax :\n",
      "    The maximum value of an array along a given axis, ignores NaNs.\n",
      "\n",
      "minimum, amin, nanmin\n",
      "\n",
      "Notes\n",
      "-----\n",
      ".. versionadded:: 1.3.0\n",
      "\n",
      "The fmax is equivalent to ``np.where(x1 >= x2, x1, x2)`` when neither\n",
      "x1 nor x2 are NaNs, but it is faster and does proper broadcasting.\n",
      "\n",
      "Examples\n",
      "--------\n",
      ">>> np.fmax([2, 3, 4], [1, 5, 2])\n",
      "array([ 2.,  5.,  4.])\n",
      "\n",
      ">>> np.fmax(np.eye(2), [0.5, 2])\n",
      "array([[ 1. ,  2. ],\n",
      "       [ 0.5,  2. ]])\n",
      "\n",
      ">>> np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan])\n",
      "array([ 0.,  0., nan])\n",
      "\u001b[1;31mClass docstring:\u001b[0m\n",
      "Functions that operate element by element on whole arrays.\n",
      "\n",
      "To see the documentation for a specific ufunc, use `info`.  For\n",
      "example, ``np.info(np.sin)``.  Because ufuncs are written in C\n",
      "(for speed) and linked into Python with NumPy's ufunc facility,\n",
      "Python's help() function finds this page whenever help() is called\n",
      "on a ufunc.\n",
      "\n",
      "A detailed explanation of ufuncs can be found in the docs for :ref:`ufuncs`.\n",
      "\n",
      "**Calling ufuncs:** ``op(*x[, out], where=True, **kwargs)``\n",
      "\n",
      "Apply `op` to the arguments `*x` elementwise, broadcasting the arguments.\n",
      "\n",
      "The broadcasting rules are:\n",
      "\n",
      "* Dimensions of length 1 may be prepended to either array.\n",
      "* Arrays may be repeated along dimensions of length 1.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "*x : array_like\n",
      "    Input arrays.\n",
      "out : ndarray, None, or tuple of ndarray and None, optional\n",
      "    Alternate array object(s) in which to put the result; if provided, it\n",
      "    must have a shape that the inputs broadcast to. A tuple of arrays\n",
      "    (possible only as a keyword argument) must have length equal to the\n",
      "    number of outputs; use None for uninitialized outputs to be\n",
      "    allocated by the ufunc.\n",
      "where : array_like, optional\n",
      "    This condition is broadcast over the input. At locations where the\n",
      "    condition is True, the `out` array will be set to the ufunc result.\n",
      "    Elsewhere, the `out` array will retain its original value.\n",
      "    Note that if an uninitialized `out` array is created via the default\n",
      "    ``out=None``, locations within it where the condition is False will\n",
      "    remain uninitialized.\n",
      "**kwargs\n",
      "    For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`.\n",
      "\n",
      "Returns\n",
      "-------\n",
      "r : ndarray or tuple of ndarray\n",
      "    `r` will have the shape that the arrays in `x` broadcast to; if `out` is\n",
      "    provided, it will be returned. If not, `r` will be allocated and\n",
      "    may contain uninitialized values. If the function has more than one\n",
      "    output, then the result will be a tuple of arrays."
     ]
    }
   ],
   "source": [
    "np.fmax??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  4,  42, 124, 731, 135])"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不能是一个数组求最大值，因为自己跟自己比有意思吗？ 哈哈\n",
    "# 必须是多个的，对应位置进行比较，然后每个对应位置最大的数字在组成一个新的数组\n",
    "np.fmax([4,3,124,13,24],[2,42,16,731,135])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  3, 16, 13, 24])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# fmin 也是同理\n",
    "np.fmin([4,3,124,13,24],[2,42,16,731,135])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2,   4,  -6, 141, 124])"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果想进行多维的比较，使用一次 fmin是不可以的。\n",
    "# 因为只能传递两个数组\n",
    "# 但是我们可以进行嵌套\n",
    "np.fmin([16,31,-6,141,2200],np.fmin([2,42,16,731,135],[5,4,132,675,124]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.,  1.,  0.,  1.,  1., -1.])"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sign()函数的使用\n",
    "arr = np.array([-4,12,0,0.12,42,-5.62])\n",
    "np.sign(arr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Data_Analysis",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
